﻿using System;
using System.Collections.Generic;
using System.Linq;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using Stockholm.SOA.Kaskelot.WCF.ServiceContracts;
using Kaskelot.SP.Services;

namespace Kaskelot.Web.Services
{
	public class WebService : BaseService<IKaskelotService>
	{
		private static log4net.ILog logger = log4net.LogManager.GetLogger("Kaskelot");
		public Val GetVal(Guid? id)
		{
			Val val = null;
			try
			{
				val = this.channel.GetVal(id);
			}
			catch (Exception ex)
			{
				logger.Error("GetVal", ex);
			}
			return val;
		}

		public Person GetPerson(Guid id)
		{
			Person p = null;
			try
			{
				p = this.channel.GetPerson(WebContext.Current.Val.Id, id);
			}
			catch (Exception ex)
			{
				logger.Error("GetPerson", ex);
			}
			return p;
		}

		public void UpdatePerson(Person p)
		{
			try
			{
				this.channel.UpdatePerson(p);
			}
			catch (Exception ex)
			{
				logger.Error("UpdatePerson", ex);
			}
		}

		public Person GetPersonByPersonnummer(Guid valId, string personnummer)
		{
			Person p = null;
			try
			{
				p = this.channel.GetPersonByPersonnummer(valId, personnummer);
			}
			catch (Exception ex)
			{
				logger.Error("GetPersonByPersonnummer", ex);
			}
			return p;
		}

		public Person GetPersonByEmail(Guid valId, string email)
		{
			Person p = null;
			try
			{
				p = this.channel.GetPersonByEmail(valId, email);
			}
			catch (Exception ex)
			{
				logger.Error("GetPersonByEmail", ex);
			}
			return p;
		}

		public ValdistriktDetailsView GetValdistriktDetailsByPersonId(Guid id)
		{
			ValdistriktDetailsView p = null;
			try
			{
				p = this.channel.GetValdistriktDetailsByPersonId(WebContext.Current.Val.Id, id);
			}
			catch (Exception ex)
			{
				logger.Error("GetValdistriktDetailsByPersonId", ex);
			}
			return p;
		}

		public GruppDetailsView GetGruppDetailsByPersonId(Guid id)
		{
			GruppDetailsView p = null;
			try
			{
				p = this.channel.GetGruppDetailsByPersonId(WebContext.Current.Val.Id, id);
			}
			catch (Exception ex)
			{
				logger.Error("GetGruppDetailsByPersonId", ex);
			}
			return p;
		}

		public Blankett GetBlankettByName(string blankettName)
		{
			Blankett b = null;
			try
			{
				b = this.channel.GetBlankettByName(WebContext.Current.Val.Id, blankettName);
			}
			catch (Exception ex)
			{
				logger.Error("GetBlankettByName", ex);
			}
			return b;
		}

		public Blankett GetBlankettById(Guid id)
		{
			Blankett b = null;
			try
			{
				b = this.channel.GetBlankett(WebContext.Current.Val.Id, id);
			}
			catch (Exception ex)
			{
				logger.Error("GetBlankettById", ex);
			}
			return b;
		}

		public Falt[] GetFaltForBlankett(Guid blankettId)
		{
			Falt[] f = null;
			try
			{
				f = this.channel.GetFaltForBlankett(WebContext.Current.Val.Id, blankettId);
			}
			catch (Exception ex)
			{
				logger.Error("GetFaltForBlankett", ex);
			}
			return f;
		}

		public FaltAlternativ[] GetFaltAlternativForFalt(Guid faltId)
		{
			FaltAlternativ[] fa = null;
			try
			{
				fa = this.channel.GetFaltAlternativForFalt(WebContext.Current.Val.Id, faltId);
			}
			catch (Exception ex)
			{
				logger.Error("GetFaltAlternativForFalt", ex);
			}
			return fa;
		}

		public void CreateBlankettPost(BlankettPost blankettPost)
		{
			try
			{
				this.channel.CreateBlankettPost(blankettPost);
			}
			catch (Exception ex)
			{
				logger.Error("CreateBlankettPost", ex);
			}
		}

		public void SetFaltDataForBlankettPost(Guid blankettPostId, IEnumerable<FaltData> faltData)
		{
			try
			{
				this.channel.SetFaltDataForBlankettPost(WebContext.Current.Val.Id, blankettPostId, faltData);
			}
			catch (Exception ex)
			{
				logger.Error("SetFaltDataForBlankettPost", ex);
			}
		}

		public IEnumerable<KursTillfalle> GetKursTillfalleByPersonId(Guid id)
		{
			IEnumerable<KursTillfalle> result = null;
			try
			{
				result = this.channel.GetKursTillfalleByPersonId(WebContext.Current.Val.Id, id);
			}
			catch (Exception ex)
			{
				logger.Error("GetKursTillfalleByPersonId", ex);
				throw;
			}
			return result;
		}

		public IEnumerable<KursTillfalle> GetKursTillfallenByKursId(Guid id)
		{
			IEnumerable<KursTillfalle> result = null;
			try
			{
				result = this.channel.GetKursTillfallenByKursId(WebContext.Current.Val.Id, id);
			}
			catch (Exception ex)
			{
				logger.Error("GetKursTillfallenByKursId", ex);
				throw;
			}
			return result;
		}

		public IEnumerable<PersonKursTillfalle> GetPersonKursTillfallenByKursTillfalleId(Guid kurstillfalleId)
		{
			IEnumerable<PersonKursTillfalle> result = null;
			try
			{
				result = this.channel.GetPersonKursTillfallenByKursTillfalleId(WebContext.Current.Val.Id, kurstillfalleId);
			}
			catch (Exception ex)
			{
				logger.Error("GetPersonKursTillfallenByKursTillfalleId", ex);
				throw;
			}
			return result;
		}

		public void UpdatePersonKursTillfalle(PersonKursTillfalle pkt)
		{
			try
			{
				this.channel.UpdatePersonKursTillfalle(pkt);
			}
			catch (Exception ex)
			{
				logger.Error("UpdatePersonKursTillfalle", ex);
				throw;
			}
		}

		public NotifieringsResultat SkickaEpost(BlankettPost bp, FaltData[] fd)
		{
			NotifieringsResultat svar = null;
			try
			{
				svar = this.channel.SkickaEpost(WebContext.Current.Val.Id, bp, fd);
			}
			catch (Exception ex)
			{
				logger.Error("SkickaEpost", ex);
				throw;
			}
			return svar;
		}
	}
}